//
//  LYVisitorView.swift
//  微博
//
//  Created by liuerying on 16/5/21.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

import SnapKit


@objc protocol  LYVisitorViewDelgate : NSObjectProtocol{
    //协议的方法
    func userWillLogin()
    
    func userWillRegister()
    
}

class LYVisitorView: UIView {
    //设置代理对象
    weak var vistorDelgate:LYVisitorViewDelgate?
  
//向外界提供一个可以设置任意界面的用户登录的访客视图
    func updateUI(title: String,imageName:String,isHome:Bool = false){
    
        if isHome{
        
            statAnimation()
        }else{
        
            largeHouse.hidden = true
            
            bringSubviewToFront(cirleIcon)
        }
        cirleIcon.image = UIImage(named: imageName)
        
        tipLabel.text = title
    }
    
    
  private  func statAnimation(){
    
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        basic.duration = 15
         basic.repeatCount = MAXFLOAT

         basic.toValue = 2*M_PI
    
         basic.removedOnCompletion = false
         cirleIcon.layer.addAnimation(basic, forKey: nil)
    
    }
 
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    //设置UI界面
    private func setUpUI(){
    
        addSubview(cirleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        cirleIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        backIcon.snp_makeConstraints { (make) in
            make.center.equalTo(cirleIcon.snp_center)
        }
        
        largeHouse.snp_makeConstraints { (make) in
            make.centerX.equalTo(cirleIcon.snp_centerX)
            make.centerY.equalTo(cirleIcon.snp_centerY)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            make.top.equalTo(cirleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(cirleIcon.snp_centerX)
        make.height.equalTo( 40)
            make.width.equalTo(230)
        }
        
        
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //按钮的点击事件
        loginBtn.addTarget(self, action: #selector(LYVisitorView.loginBtnClick), forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: #selector(LYVisitorView.registerBtnClick), forControlEvents: .TouchUpInside)
    }
    
    //"点击了登录按钮"
   @objc private func loginBtnClick(){
    
    vistorDelgate?.userWillLogin()
    
    }
    
    //点击了注册按钮
    @objc private func registerBtnClick(){
        //在合适的时候调用协议中的方法
      vistorDelgate?.userWillRegister()
        
    }
    //懒加载子视图
    private lazy var cirleIcon:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var backIcon:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
        private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    private lazy var tipLabel:UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜", font: 14, textcolor: UIColor.darkGrayColor(), alignment: .Center)
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
       
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
      
      //切图
        let image = UIImage(named: "common_button_white_disable")!
        
        let W = Int(image.size.width * 0.5)
        let H = Int(image.size.height * 0.5)

        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(W, topCapHeight: H), forState: UIControlState.Normal)
     
        
        return btn
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)

        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)

        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        
        return btn
    }()
    

}
